<?php
class SuppliersController extends AppController {
       var $name = 'Suppliers';
       var $helpers = array('Html','Form');
   //    var $uses = array('Supplier','Producto');
        
        function index(){
             $this->autorizado($this->Session->read('Usuario.grupoId'),"suppliers");
                   $this->set('suppliers', $this->Supplier->find('all', array('conditions' => array('Supplier.activo' => 1),
                       'limit'=> 10,
                       'order'=>'Supplier.nombre ASC',)));
        }
                function indexcompleto(){
                   $this->set('suppliers', $this->Supplier->find('all', array('conditions' => array('Supplier.activo' => 1
                       ),'order'=>'Supplier.nombre ASC',)));
        }
        
        function add(){
             $this->autorizado($this->Session->read('Usuario.grupoId'),"suppliers");
            if(!empty($this->data))
            {
                $this->Supplier->create();
                if($this->Supplier->save($this->data))
                {
                    $this->Session->setFlash('Registro guardado exitosamente');
                    $this->redirect(array('action'=>'index'),null);
                }
             else
             {
                    $this->Session->setFlash('Problemas al realizar el registro, intentelo nuevamente');
             }
            }
        }
        
        function edit($id = null){
             $this->autorizado($this->Session->read('Usuario.grupoId'),"suppliers");
            if(!$id) //Compruebo si he pasado el parametro ( POST )
            {
                $this->Session->setFlash('Proveedor incorrecto');
                $this->redirect(array('action'=>'index'),null,true);
            }

            if(empty ($this->data))
            {
                $this->data = $this->Supplier->find('first',array('conditions' => array('Supplier.id' => $id)));   
             
            }
            else
            {
                if($this->Supplier->save($this->data))
                {
                    $this->Session->setFlash("Datos modificados");
                    $this->redirect(array('action'=>'index'),null,true);
                }
                else
                {
                     $this->Session->setFlash('Problemas al realizar la modificación, intentelo nuevamente');
                }
            }
        }
        
        function delete($id = null){
             $this->autorizado($this->Session->read('Usuario.grupoId'),"suppliers");
            if(!$id)
            {
                $this->Session->setFlash("Problema al eliminar proveedor");
                $this->redirect(array('action'=>'index'),null,true);
            }
            else
            {
                //Proc almacenado, ver en /Documentacion/Script BD/Proc....
                //Se coloca asi para evitar inyeccion sql
                
                
                $query = "call eliminar_supplier( false, " .$id . ");" ;
                
                $this->Supplier->query($query);
                $this->Session->setFlash("Proveedor eliminado");
                $this->redirect(array('action' => 'index'),null, true);
            }
            
        }
}
?>